package com.springboot.framework.mvc.mapper.driver;

import com.springboot.framework.exception.BaseException;
import com.springboot.framework.mvc.mapper.config.MybatisConfig;
import com.springboot.framework.mvc.mapper.config.MybatisMapperRegistry;
import org.apache.ibatis.session.Configuration;


public class BaseMapperDriverResolver extends DriverResolver {

    public String createSqlSource(Configuration configuration, String script){
        //获取当前mapper
        Class<?> mapperClass = null;
        if(configuration instanceof MybatisConfig){
            mapperClass = MybatisMapperRegistry.getCurrentMapper();
            if(mapperClass == null){
                throw new BaseException("解析SQL出错");
            }
        }
        //处理SQL
        if(mapperClass!=null) {
            /* 获取当前mapper接口上的泛型,这个泛型实际上对应着实体类 */
            Class<?> generics = getMapperGenerics(mapperClass);
            String newScript = super.commonCreateSqlSource(generics, script);
            StringBuilder sb = new StringBuilder( "<script>");
            sb.append(newScript);
            sb.append("</script>");
            script = sb.toString();
        }
        return script;
    }

}
